package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.dao.impl.DesignerDaoImpl;
import com.fk.entity.Designer;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.Result;
import com.google.gson.Gson;

public class AdminRoleCategroyServlet extends BaseServlet {
	/**
	 * 鏌ヨ鎵�鏈夎璁″笀浜哄憳
	 * localhost:8080/wuyijiuchaoliufushiguan/adminRoleCategroy?reqName=getRoleCates
	 * 
	 * **/
	
	public void getRoleCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DesignerDaoImpl deDao = new DesignerDaoImpl();//鍒涘缓瀹炵幇绫诲璞�
		List<Designer> deList = deDao.getDesignerList();//鏌ュ埌鎵�鏈夎璁″笀闆嗗悎
		
		//鍙戦�乤jax璇锋眰锛屾湇鍔″櫒鍩烘湰閮戒細杩斿洖json鏍煎紡瀛楃涓�
		
		response.setCharacterEncoding("UTF-8");//璁剧疆鍝嶅簲鐨勭紪鐮佹牸寮�
		PrintWriter out = response.getWriter();//out瀵硅薄
		
		Gson gson = new Gson();
		String res = gson.toJson(deList);//灏哾eList杞寲涓簀son瀛楃涓�
		
		out.print(res);//鍝嶅簲鏁版嵁
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/wuyijiuchaoliufushiguan/adminRoleCategroy?reqName=delRoleCate
	 * **/
	
	public void delRoleCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int rId = Integer.parseInt(request.getParameter("designerId"));//鎺ュ彈鍓嶅彴浼犳潵鐨勫弬鏁�
		DesignerDaoImpl deDao = new DesignerDaoImpl();
		int n = deDao.deleteRoleCateById(rId);//璋冪敤鍒犻櫎鐨勬柟娉�
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (n>0) {
			out.print(Result.toClient(true, "鍒犻櫎鎴愬姛", ""));
		} else {
			out.print(Result.toClient(false, "鍒犻櫎澶辫触", ""));
		}
		out.flush();
		out.close();
		
	}
	/**
	 * localhost:8080/wuyijiuchaoliufushiguan/adminRoleCategroy?reqName=addRoleCate
	 * **/
	
	public void addRoleCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		DesignerDaoImpl deDao = new DesignerDaoImpl();//产品分类的实现类对象
		
		String name = request.getParameter("name");//接收前台传来的参数
		String position = request.getParameter("position");
		String img = request.getParameter("img");
		if (name!=null && !name.equals("")) {
			List<Designer> deList = deDao.getDesignerByName(name);
			if (deList.size() == 0) {//没有查到，可以添加
				Designer de = new Designer(0, name, position, img);//根据接收的参数创建产品分类对象
				int d = deDao.addDesigner(de);
				if (d > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {//查到了，不能添加
				out.print(Result.toClient(false, "设计师已存在", ""));
			}
		}
		
		
		out.flush();
		out.close();
	}

	private void getDesignerList() {
		// TODO Auto-generated method stub
		
	}

	
}
